$(function () {
    //      函数调用
    initArtCateList()

    //封装函数
    function initArtCateList() {

        axios({

            method: 'GET',
            url: '/my/article/cates'
        }).then(res => {
            console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            // 成功渲染直接渲染
            let arr = []
            res.data.data.forEach(function (ele) {
                arr.push(`
                
                        <tr>
                        <td>${ele.Id}</td>

                        <td>${ele.name}</td>

                        <td>${ele.alias}</td>
                        <td>
                            <button data-id='${ele.Id}'  class="btn-edit layui-btn layui-btn-xs">修改</button>
                            <button data-id='${ele.Id}' class="del-edit layui-btn layui-btn-xs layui-btn-danger">删除</button>

                        </td>
                    </tr>
                `
                )
            })

            $('tbody').empty().html(arr.join(''))
        })
    }
    let indexAdd = 0
    // 需求2 点击按钮 添加对话框
    $('#addBtn').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ['500px', '260px'],
            //content要放form表单 放到末尾
            content: `
                    <form id='addForm' class="layui-form">
                    <input type="hidden" name="Id" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                        <input type="text" name="name" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                        <input type="text" name="alias" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                        </div>
                    </div>
                </form>`
        })
    })

    //添加文章分类 -后添加动态元素  事件委托
    $('body').on('submit', '#addForm', function (e) {

        e.preventDefault()//阻止默认事件
        //preventDefault()[dom标准写法(ie678不兼容)]

        // alert(555)
        //添加分类
        axios({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            //成功提示
            layer.msg('恭喜成功')
            initArtCateList()

            //关闭对话框
            layer.close(indexAdd)
        })
    })

    $('tbody').on('click', '.btn-edit', function () {
        indexEdit = layer.open({

            type: 1,
            title: '修改文章类别',
            area: ['31.25rem', '16.25rem'],
            //content要放form表单 放到末尾
            content: `
                    <form id='editForm' class="layui-form" lay-filter='editForm'>
                    <input type="hidden" name="Id" >
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                        <input type="text" name="name" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                        <input type="text" name="alias" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                        </div>
                    </div>
                 </form>`
        })

        //点击编辑按钮做两件事 2，赋值 以及上面的按钮加Id
        let id = $(this).attr('data-id')
        //  alert(id)
        axios({
            method: 'GET',
            //id是动态参数 要+
            url: '/my/article/cates/' + id

        }).then(res => {
            console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            // 成功渲染后.
            layui.form.val('editForm', res.data.data)
        })


    })
    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()//阻止默认事件
        //            // alert(555)
        //修改分类
        axios({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            //成功提示
            layer.msg('恭喜成功 修改成功')
            initArtCateList()

            //关闭对话框
            layer.close(indexEdit)
        })
    })


    //删除按钮
    $('tbody').on('click', '.del-edit', function () {

        //获取自定义属性
        let id = $(this).attr('data-id')
        // alert(id)

        layer.confirm('确认删除？', { icon: 3, title: '提示' }, function (index) {
            axios({

                url: '/my/article/deletecate/' + id
            }).then(res => {
                console.log(res);
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message)
                }
                //成功提示
                layer.msg('删除成功')
                initArtCateList()


            })
            layer.close(index)
        })
    })



})
